package com.core.comment;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigUtil {
    private static Properties properties = new Properties();

    static {
        doLoadConfig("application.properties");
    }

    public static void put(String key, String value) {
        properties.put(key, value);
    }

    public static String get(String key) {
        return properties.getProperty(key);
    }

    public static void doLoadConfig(String location) {

        System.out.println("配置文件路径:" + ConfigUtil.class.getClassLoader().getResource(location).getPath());
        InputStream resourceAsStream = ConfigUtil.class.getClassLoader().getResourceAsStream(location);

        try {
            //用Properties文件加载文件里的内容
            if (resourceAsStream != null) {
                properties.load(resourceAsStream);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关流
            if (null != resourceAsStream) {
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
